﻿;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;~ 作         者:  河许人 && 天黑请闭眼
;~ 脚本说明： 影子输入法，承影（texter热字符，）_hello_srf柚子输入法，输入要做的就是
;~ 环境 版本:   Autohotkey v1.1.32.00 Win10Pro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 部分自定义设置（热键修改等）可以写入主目录\Lib\usercustoms.ahk文件中
#NoEnv
#MaxMem 128
#KeyHistory 0
#SingleInstance Force
#MaxHotkeysPerInterval 400	; receive 400 Hotkeys per 2 seconds

OnExit, Exit
ListLines, Off
SetBatchLines, -1
SetKeyDelay, 0, 0
Process, Priority,, High
CoordMode, Caret, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen
SetWorkingDir %A_ScriptDir%
OnClipboardChange("ClipChanged")
OnMessage(0x201, "WM_LBUTTONDOWN")
OnMessage(0x4a, "Receive_WM_COPYDATA")
CloseOtherYZ()
func_key:="/"
srf_symbol:={"``":["``","·"], "~":["~","~"], "!":["!","！"], "@":["@","@"], "#":["#","#"], "$":["$","￥"], "%":["%","%"], "^":["^","……"], "&":["&","&"], "*":["*","*"], "(":["(","（）{Left}"], ")":[")","）"]
, "_":["_","——"], "-":["-","-"], "+":["+","+"], "=":["=","="], "[":["[","【】{Left}"], "]":["]","】"], "{":["{","{}{Left}"], "}":["}","}"], "\":["\","、"], "|":["|","|"], ";":[";","；"], ":":[":","："]
, "'":["'","‘’{Left}"], """":["""","“”{Left}"], "<":["<","《》{Left}"],">":[">","》"],",":[",","，"],".":[".","。"], "/":["/","/"], "?":["?","？"]}
Gosub srf_init
SetTimer, EmptyMem, 60000
EmptyMem()
Return

Registrationhotkey:		; 注册快捷键
	Try {								; 注册切换热键
		Hotkey If, !(srf_inputing&&Switch~="Shift")
		Hotkey, %Switch%, srfSwitch, On
		If !InStr(Switch,"&")&&(Switch~="i)(L|R)?(Win|Alt|Shift|Control|Ctrl|CapsLock)")
			Try Hotkey, % "~" Trim(Switch,"+!#^") " & XButton2", AndLButton, On
		Hotkey If
	} Catch
		MsgBox, 16, 错误, 注册切换热键失败，请手动在[选项>控制]中设置其他热键！！！, 3

	cncharmode_funobj:=Func("cncharmode").Bind()
	Hotkey If, % cncharmode_funobj
	Loop 26 {
		Hotkey, % Chr(96+A_Index), cnInputchar, On
		Hotkey, % "+" Chr(96+A_Index), cnInputchar, On
	}
	For key, value In srf_symbol
		Hotkey, %key%, Sendsymbol, On
	Hotkey, If

	Hotkey, If, srf_inputing
	Loop 10 {
		Hotkey, % A_Index-1, srf_select_ci, On
		Hotkey, % "Numpad" A_Index-1, srf_select_ci, On
	}
	Loop 26
		Hotkey, % "^" Chr(96+A_Index), searchinsertpos
	Hotkey, PgUp, lessWait, On
	Hotkey, PgDn, MoreWait, On
	Hotkey, Space, , T2
	Hotkey, If
	; Speed Test
	Hotkey If, waitspace
	Loop 26
		Hotkey, % "Space & " Chr(96+A_Index), dwselect, On
	Hotkey, If

	; Loop 26
	; 	Hotkey, % Chr(96+A_Index), Test, On
	; Hotkey, Space, Test, On
Return
; Test:
; Return
cncharmode(){
	global srf_mode, srf_inputing
	Return srf_inputing||(srf_mode&&!GetKeyState("CapsLock", "T"))
}

!^F12::
srfsuspend:
	Suspend
	Gosub srf_value_off
	If (A_IsSuspended){
		srf_mode:=0
		Gui, 2:Hide
		Menu, Tray, Rename, 7&, 恢复
		Menu, Tray, Icon, Lib\yz.icl, 2, 1
		SetTimer, AppImeAutoSwitch, Off
		SetIMEStatus(1,1)
		SetTimer, ToolTipInputStatus, -10
	} Else {
		srf_mode:=1
		Gui, 2:Show, NA
		Menu, Tray, Rename, 7&, 禁用
		Menu, Tray, Icon, Lib\yz.icl, 1, 1
		SetTimer, AppImeAutoSwitch, 300
	}
Return

#If !(srf_inputing&&Switch~="Shift")
srfSwitch:
	HoldKey:=RegExReplace(A_ThisHotkey,"i)^([<>$\^!+#]+)?([a-z ]+&[ ]+)?([a-z\d]+)$","$3")
	If (HoldKey!=A_ThisHotkey)
		KeyWait, % HoldKey
	If !Double||(A_ThisHotkey&&(A_PriorHotkey=A_ThisHotkey)&&(A_TimeSincePriorHotkey<400)){
		If (srf_mode:=!srf_mode){
			Gui, 2:Show, NA
		} Else {
			Gui, 2:Hide
			If (Shiftfg=2)&&(A_ThisHotkey~="Shift$")&&srf_inputing
				SendInput(srf_all_Input, SendDelaymode)
			Gosub srf_value_off
		}
		SetTimer, ToolTipInputStatus, -100
	}
Return
#If
AndLButton:
	Suspend, Permit
Return

#If srf_inputing&&!InStr(srf_all_Input,func_key)
	^1::
	^2::
	^3::
	^4::
	^5::
	^6::
	^7::
	^8::
	^9::
	^0::srf_SetFirst(SubStr(A_ThisHotkey,0))

	^!1::
	^!2::
	^!3::
	^!4::
	^!5::
	^!6::
	^!7::
	^!8::
	^!9::
	^!0::srf_delete(SubStr(A_ThisHotkey,0))
#If

; 空格、0-9 定义
#If srf_inputing
	Space::
		If srf_symbol[srf_all_Input,2-(EnSymbol||Englishmode)] {
			RegExMatch(srf_symbol[srf_all_Input,2-(EnSymbol||Englishmode)],"i)\{[a-z]+\}",Match)
			SendInput(StrReplace(srf_symbol[srf_all_Input,2-(EnSymbol||Englishmode)],Match))
			If (Match)
				SendInput, %Match%
			Gosub srf_value_off
		} Else
			select_for_num(1)
	Return
	srf_select_ci:
		select_for_num(SubStr(A_ThisHotkey,0))
	Return

	fencifu:
		If ((insertpos>1?SubStr(srf_all_Input, insertpos-1, 1):"") SubStr(srf_all_Input, Max(0,insertpos), 1)~="['\\``]")
			Return
		If (A_ThisHotkey="\")&&!InStr(srf_all_Input, func_key){
			If !InStr(srf_all_Input_raw_for_trim,"'")
				Return
			If !InStr(srf_all_Input,"\")
				qiecistr:=save_field_array[1,0]
			qiecistr:=RegExReplace(Trim(qiecistr,"'"),"(^|')[a-z]+$")
			If (qiecistr){
				RegExMatch(srf_all_Input_raw_for_trim "'", "([a-z]+'){" StrSplit(qiecistr,"'").Length() "}", tvar)
				srf_all_Input:=RegExReplace(StrReplace(srf_all_Input,"\"), "(" StrReplace(tvar,"'","'?") ")","$1\", , 1)
			} Else
				srf_all_Input:=StrReplace(srf_all_Input,"\"), save_field_array.RemoveAt(1)
			jichu_for_select_Array:=pinyinmethod(srf_all_Input,Inputscheme)
			Gosub houchuli
			Gosub srf_tooltip_fanye
		} Else {
			srf_all_Input:=insertchar(srf_all_Input,A_ThisHotkey,insertpos), waitnum:=0
			Gosub srf_tooltip
		}
	Return

	;srf_all_input模式 backspace键、esc键、enter键、Lshift键 定义
	Delete::
		srf_all_Input:=delchar(srf_all_Input,insertpos,1), waitnum:=0, modeMatch:=tfzm:=""
		If (srf_all_Input = "")
			Gosub srf_value_off
		Else If (srf_all_Input = func_key){
			ToolTipText:=func_key, srf_for_select:="(" func_key ") " srf_symbol[func_key,2-(EnSymbol||Englishmode)]
			Gosub showhouxuankuang
		} Else
			Gosub srf_tooltip
	Return
	BackSpace::
		If (dwselect){
			If (tfzm="")
				dwselect:=0
			Else {
				tfzm:=SubStr(tfzm, 1, -1)
				jichu_for_select_Array:=pinyinmethod(srf_all_Input, Inputscheme), waitnum:=0
				Gosub houchuli
				Gosub srf_tooltip_fanye
			}
			Return
		}
		srf_all_Input:=delchar(srf_all_Input,insertpos), waitnum:=0, modeMatch:=tfzm:=""
		If (srf_all_Input = "")
			Gosub srf_value_off
		Else If (srf_all_Input = func_key){
			ToolTipText:=func_key, srf_for_select:="(" func_key ") " srf_symbol[func_key,2-(EnSymbol||Englishmode)]
			Gosub showhouxuankuang
		} Else
			Gosub srf_tooltip
	Return

	Tab::
		If fuzhuma&&(jichu_for_select_Array[1,0]~="pinyin")
			dwselect:=1
		Else
			Gosub MoreWait
	Return
	
	Shift::
		Critical, Off
		KeyWait, Shift
		If (A_ThisHotkey="Shift")&&(Shiftfg<4||shurulei!="pinyin"){
			Learnfg:=0
			If (Shiftfg&2=2){
				SendInput(srf_all_Input, SendDelaymode)
				Gosub srf_value_off
				If (Shiftfg=3){
					Gui, 2:Hide
					srf_mode:=0
					SetTimer, ToolTipInputStatus, -10
				}
			} Else
				Gosub srf_value_off
		} Else If !tfzm&&(shurulei="pinyin"&&fuzhuma){
			If (A_PriorHotkey!="Shift")&&RegExMatch(srf_all_Input_raw_for_trim,"O).*[a-z][a-z]'([a-z]('?[a-z])?)'$",fzm){
				tfzm:=StrReplace(fzm.Value[1],"'"), srf_all_Input:=RegExReplace(srf_all_Input, tfzm "$")
				jichu_for_select_Array:=pinyinmethod(srf_all_Input, Inputscheme), waitnum:=0
				Gosub houchuli
				Gosub srf_tooltip_fanye
			} Else If (A_PriorHotkey="Shift")&&(A_ThisHotkey="Shift"){
				srf_mode:=Learnfg:=0
				Gosub srf_value_off
				Gui, 2:Hide
				SetTimer, ToolTipInputStatus, -10
			}
		} Else If (A_ThisHotkey="Shift"){
			srf_mode:=Learnfg:=0
			Gosub srf_value_off
			Gui, 2:Hide
			SetTimer, ToolTipInputStatus, -10
		}	
	Return

	Enter::
	NumpadEnter::
		If (Enterfg=2)
			SendInput(srf_all_Input, SendDelaymode)
		Gosub srf_value_off
	Return
#If

; srf_mode模式 a-z键、esc键、中文符号 定义
#If (srf_inputing||(Escfg>1&&srf_mode))
Esc::
	If (!srf_inputing){
		srf_mode:=0
		Gui, 2:Hide
		SetTimer, ToolTipInputStatus, -10
	}
	Gosub srf_value_off
	If (srf_mode&&Escfg=3){
		srf_mode:=0
		Gui, 2:Hide
		SetTimer, ToolTipInputStatus, -10
	}
Return
#If
#If srf_mode&&(InStr(srf_all_Input,func_key))
NumpadDot::
	If (srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring")||(srf_all_Input~="^" func_key "[a-z]+$"){
		srf_HotStringSelect(Function_for_select, 4)
	} Else If (srf_func_table[modeMatch.Value[1], "Name"]="get_function")||!(modeMatch.Value[1]=""||srf_Default_Func[srf_func_table[modeMatch.Value[1], "Name"],2]~="\d"){
		srf_RunSelect(Function_for_select, 4)
	} Else {
		If (SubStr(srf_all_Input, 0)=func_key||srf_all_Input=func_key)
			Return
		srf_all_Input:=insertchar(srf_all_Input,".",insertpos)
		Gosub srf_tooltip
	}
Return
NumpadDiv::
NumpadMult::
NumpadAdd::
NumpadSub::
	srf_all_Input:=insertchar(srf_all_Input,(A_ThisHotkey="NumpadDiv"?"/":A_ThisHotkey="NumpadMult"?"*":A_ThisHotkey="NumpadAdd"?"+":"-"),insertpos)
	Gosub srf_tooltip
Return
#If
#If srf_mode
#If
^=::srf_create(RegExReplace(srf_all_Input,"i)[^a-z]"))
^+!F1::
	; Run, % "https://gitee.com/orz707/shadow/wikis/%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B?sort_id=1844118"
	Gosub help
Return
#If srf_mode&&!srf_inputing
	~1::
	~2::
	~3::
	~4::
	~5::
	~6::
	~7::
	~8::
	~9::
	~0::
	numberfg:=1
	Return
#If

Inputchar:
	SendInput(GetKeyState("CapsLock", "T")^GetKeyState("Shift", "P")?Format("{:U}", LTrim(A_ThisHotkey,"+")):LTrim(A_ThisHotkey,"+"))
Return
waitspace:
	Critical, Off
	If GetKeyState("Space", "P")&&!(srf_all_Input~="^'|" func_key){
		GdipText(ToolTipText, srf_for_select, Caret.X, Caret.Y+30, TextFont)
		KeyWait, Space, U
		GdipText(ToolTipText, srf_for_select, Caret.X, Caret.Y+30, TextFont)
	}
Return

#If srf_mode&&!(GetKeyState("CapsLock")&&!srf_inputing)
; 设置热键
; a-z定义
; 设置热键
cninputchar:
	RunTime:=A_TickCount
	If StrLen(srf_all_Input)>100
		Return
	If (dwselect){
		tfzm:=StrLen(tfzm)=2?LTrim(A_ThisHotkey,"+"):tfzm LTrim(A_ThisHotkey,"+"), jichu_for_select_Array:=pinyinmethod(srf_all_Input, Inputscheme), waitnum:=0
		Gosub houchuli
		Gosub srf_tooltip_fanye
	} Else {
		If (tfzm)
			srf_select(1,,,1), tfzm:=""
		srf_all_Input:=insertchar(srf_all_Input,GetKeyState("CapsLock", "T")^GetKeyState("Shift", "P")?Format("{:U}", SubStr(A_ThisHotkey,0)):SubStr(A_ThisHotkey,0),insertpos)
		waitnum:=numberfg:=0, srf_for_select:="", srf_inputing:=1
		; If !InStr(srf_all_Input, func_key)
		; 	srf_all_Input:=RegExReplace(srf_all_Input,"([aeio])gn","$1ng")
		Gosub srf_tooltip
	}
	If (dwxg&&shurulei="pinyin"&&ToolTipStyle=2)
		If (jichu_for_select_Array[1,-2]=""&&jichu_for_select_Array[1,0]~="pinyin"&&StrLen(jichu_for_select_Array[1,2])>1){
			waitspace:=1
			SetTimer, waitspace, 200
		} Else If (waitspace){
			waitspace:=0
			SetTimer, waitspace, Off
		}
	If (DebugLevel=4)
		DebugOutput(A_TickCount-RunTime "ms", 4)
Return

Sendsymbol:
	If GetKeyState("Shift", "P"){
		Switch A_ThisHotkey
		{
			Case "/":
				ThisHotkey:="?"
			Case ";":
				ThisHotkey:=":"
			Case "'":
				ThisHotkey:=""""
			Case ",":
				ThisHotkey:="<"
			Case ".":
				ThisHotkey:=">"
			Case "\":
				ThisHotkey:="|"
			Case "[":
				ThisHotkey:="{"
			Case "]":
				ThisHotkey:="}"
			Case "-":
				ThisHotkey:="_"
			Case "=":
				ThisHotkey:="+"
			Case "``":
				ThisHotkey:="~"
			Default:
				ThisHotkey:=A_ThisHotkey
		}
	} Else
		ThisHotkey:=A_ThisHotkey
	If (srf_all_Input="")&&!InStr(StrReplace(fanyefg,","), ThisHotkey){
		srf_inputing:=0
		SetTimer, removelianxiang, Off
		Gosub removelianxiang
	}
	If srf_inputing {
		If (ThisHotkey=func_key){
			If (srf_all_Input=func_key){
				Gosub srf_value_off
				SendInput(srf_symbol[func_key,2-(EnSymbol||Englishmode)])
			} Else If (SubStr(srf_all_Input,0)!=func_key)&&!(srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring"){
				srf_all_Input:=insertchar(srf_all_Input,func_key,insertpos), waitspace:=0
				Gosub srf_tooltip
			}
		} Else If (ThisHotkey=","){
			If hotstring_for_select[1, 1]&&!InStr(srf_all_Input,func_key)
				srf_HotStringSelect(hotstring_for_select)
			Else {
				If (!InStr(srf_all_Input,func_key)){
					If (bddp)
						srf_select(1, IsPinyin), SendInput(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)])
					Else
						srf_select(3, IsPinyin)
				} Else If (srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring")||(srf_all_Input~="^" func_key "[a-z]+$")
					srf_HotStringSelect(Function_for_select, 3)
				Else If InStr(srf_all_Input,"("){
					srf_all_Input:=insertchar(srf_all_Input,",",insertpos)
					Gosub srf_tooltip
				} Else
					srf_RunSelect(Function_for_select, 3)
				Return
			}
		} Else If (ThisHotkey="."){
			If Function_for_select[1, 1]&&!InStr(srf_all_Input,func_key)
				srf_RunSelect(Function_for_select)
			Else If (!InStr(srf_all_Input,func_key)){
				If (bddp)
					srf_select(1, IsPinyin), SendInput(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)])
				Else
					srf_select(4, IsPinyin)
			} Else {
				If (srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring")||(srf_all_Input~="^" func_key "[a-z]+$")
					srf_HotStringSelect(Function_for_select, 4)
				Else If (srf_func_table[modeMatch.Value[1], "Name"]="get_function")||!(modeMatch.Value[1]=""||srf_Default_Func[srf_func_table[modeMatch.Value[1], "Name"],2]~="\d")
					srf_RunSelect(Function_for_select, 4)
				Else {
					If (SubStr(srf_all_Input,0)=func_key||srf_all_Input=func_key)
						Return
					srf_all_Input:=insertchar(srf_all_Input,ThisHotkey,insertpos)
					Gosub srf_tooltip
				}
			}
		} Else If (ThisHotkey="'"){
			If (srf_all_Input="'"){
				Gosub srf_value_off
				RegExMatch(srf_symbol[ThisHotkey,(srf_mode&&!(EnSymbol||Englishmode)&&!GetKeyState("CapsLock", "T"))+1],"i)\{[a-z]+\}",Match)
				SendInput(StrReplace(srf_symbol[ThisHotkey,(srf_mode&&!(EnSymbol||Englishmode)&&!GetKeyState("CapsLock", "T"))+1],Match))
				If (Match)
					SendInput, %Match%
			} Else If ((!InStr(srf_all_Input,func_key)&&Inputscheme="pinyin")||(IsPinyin))
				Gosub fencifu
			Else {
				If (!InStr(srf_all_Input,func_key)){
					; If (bddp){
					; 	srf_select(1, IsPinyin)
					; 	RegExMatch(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)],"i)\{[a-z]+\}",Match)
					; 	SendInput(StrReplace(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)],Match))
					; 	If (Match)
					; 		SendInput, %Match%
					; } Else
						srf_select(3, IsPinyin)
				} Else If (srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring")||(srf_all_Input~="^" func_key "[a-z]+$")
					srf_HotStringSelect(Function_for_select, 3)
				Else
					srf_RunSelect(Function_for_select, 3)
				Return
			}
		} Else If (ThisHotkey="\"){
			If (Inputscheme~="pinyin|sp$"&&!bddp)
				Gosub fencifu
			Else {
				srf_select(1, IsPinyin), SendInput(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)])
			}
		} Else If (ThisHotkey=";"){
			If (Inputscheme="sgsp"){
				srf_all_Input:=srf_all_Input ";", waitnum:=numberfg:=0
				Gosub srf_tooltip
			} Else {
				If (!InStr(srf_all_Input,func_key)){
					; If (bddp)
					; 	srf_select(1, IsPinyin), SendInput(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)])
					; Else
						srf_select(2, IsPinyin)
				} Else If (srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring")||(srf_all_Input~="^" func_key "[a-z]+$")
					srf_HotStringSelect(Function_for_select, 2)
				Else
					srf_RunSelect(Function_for_select, 2)
				Return
			}
		} Else If (Learning&&shurulei="xingma"&&ThisHotkey="``"){
			Gosub fencifu
		} Else If (SubStr(srf_all_Input,1,1)=func_key&&!(srf_all_Input~="i)^" func_key "[a-z]+$")&&ThisHotkey~="[\.:\+\-=\*/\(\)]"){
			srf_all_Input:=insertchar(srf_all_Input,ThisHotkey,insertpos)
			Gosub srf_tooltip
		} Else If InStr(fanyefg, ThisHotkey){
			If (ThisHotkey~="\-|\[")
				Gosub lessWait
			Else
				Gosub MoreWait
		} Else If !InStr(srf_all_Input, func_key){
			srf_select(1, IsPinyin)
			Gosub srf_value_off
			RegExMatch(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)],"i)\{[a-z]+\}",Match)
			SendInput(StrReplace(srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)],Match))
			If (Match)
				SendInput, %Match%
		} Else {
			srf_all_Input:=insertchar(srf_all_Input,ThisHotkey,insertpos)
			Gosub srf_tooltip
		}
	} Else {
		If (ThisHotkey=".")&&(numberfg){
			Send {.}
		} Else If (ThisHotkey=func_key){
			If (srf_mode){
				srf_all_Input:=func_key, ToolTipText:=ThisHotkey, srf_for_select:="(" ThisHotkey ") " srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)], srf_inputing:=1
				Gosub showhouxuankuang
			} Else
				SendInput(srf_symbol[ThisHotkey,1])
		} Else If (ThisHotkey="'"){
			If (srf_mode){
				srf_all_Input:="'", ToolTipText:=ThisHotkey, srf_for_select:="(" ThisHotkey ") " srf_symbol[ThisHotkey,2-(EnSymbol||Englishmode)], srf_inputing:=1
				Gosub showhouxuankuang
			} Else
				SendInput(srf_symbol[ThisHotkey,1])
		; } Else If srf_mode&&(ThisHotkey=";")&&!(Inputscheme~="sp$|pinyin"){
		; 	srf_all_Input:=";", waitnum:=numberfg:=0
		; 	Gosub srf_tooltip
		} Else If (Learning&&shurulei="xingma"&&ThisHotkey="``"){
			srf_all_Input:="``", srf_inputing:=1
			Gosub srf_tooltip
		} Else {
			RegExMatch(srf_symbol[ThisHotkey,(srf_mode&&!(EnSymbol||Englishmode)&&!GetKeyState("CapsLock", "T"))+1],"i)\{[a-z]+\}",Match)
			SendInput(StrReplace(srf_symbol[ThisHotkey,(srf_mode&&!(EnSymbol||Englishmode)&&!GetKeyState("CapsLock", "T"))+1],Match))
			If (Match)
				SendInput, %Match%
		}
	}
	numberfg:=0
Return
#If
#If waitspace
dwselect:
	srf_select(Ord(SubStr(A_ThisHotkey,0))-96, IsPinyin, SubStr(A_ThisHotkey,0)), dwselect:=srf_inputing&&fuzhuma
Return
#If
#If srf_inputing
searchinsertpos:
	insertpos:=InStr(srf_all_Input, SubStr(A_ThisHotkey,0), , Max(1,insertpos)), insertpos+=(insertpos>0)
	Gosub srf_tooltip_fanye
Return
Left::
Right::
	tfzm:="", dwselect:=0
	If (!insertpos){
		If (A_ThisHotkey="Left")
			insertpos:=StrLen(srf_all_Input)
		Else
			insertpos:=1
	} Else {
		If (A_ThisHotkey="Left"){
			insertpos:=Max(1,insertpos-1)
		} Else
			insertpos:=Min(insertpos+1,StrLen(srf_all_Input)+1)
	}
	Gosub srf_tooltip_fanye
Return

F5 Up::
	Caret:=GetCaretPos(0), hasCaretPos:=1
	If (ToolTipStyle=1)
		ToolTip(1, ToolTipText "`n" srf_for_select, "x" Caret.x " y" Caret.y+Caret.h)
	Else
		GdipText(ToolTipText, srf_for_select, Caret.X, Caret.Y+30, srf_all_Input~="^" func_key "[a-z]+$"?SymbolFont:TextFont)
Return
#If

~CapsLock::SetTimer, ToolTipInputStatus, -100
ToolTipInputStatus(){
	global srf_inputing, srf_mode
	If (!srf_inputing){
		If (!srf_mode)
			SetIMEStatus()
		If (Caret:=GetCaretPos()).t="Mouse"
			ToolTip
		Else {
			ToolTip, % (!(srf_mode&&srf_inputing)&&GetKeyState("CapsLock", "T")?"Ａ":srf_mode||IME_GETOPENSTATUS()?"中":"英"), Caret.X+5, Caret.Y+30
			SetTimer, ToolTipOff, -1000
		}
	}
}
#If MouseCross
~LButton Up::
Return
#If
#If A_PriorHotkey="~LButton Up"&&A_TimeSincePriorHotkey<1200
Ctrl::selectmenu()
#If
#If ClipSaved
$^v::
recoverclip:
	Clipboard:=ClipSaved, ClipSaved:=""
	SendInput, ^v
Return
ClipChanged(Type){
    global ClipSaved
	ClipSaved:=""
	If DllCall("IsClipboardFormatAvailable","UInt",1)
		Clipboardhistory("+")
}
#If

ToolTipOff:
	ToolTip
Return

; 窗口拖动
WM_LBUTTONDOWN(l,w,msg,hWnd){
	global ToolTipStyle, @TSF, Caret
	ToolTip
	If (A_Gui=2)
		PostMessage, 0xA1, 2
	Else If (hWnd=@TSF){
		PostMessage, 0xA1, 2
		KeyWait, LButton
		WinGetPos, X, Y, , , % "ahk_id" @TSF
		Caret.X:=X, Caret.Y:=Y-30
	}
}
SaveDB(wait:=0){
	Critical %wait%
	global Sourceurl
	If (FileGetSize(A_Temp "\yzime_sql.tmp")>50){
		If FileExist(A_ScriptDir "\Lib\DBService.exe"){
			If (wait){
				RunWait %A_ScriptDir%\Lib\DBService.exe Synchronizedb
			} Else
				Run %A_ScriptDir%\Lib\DBService.exe Synchronizedb
		} Else
			UrlDownloadToFile(Sourceurl "/Lib/DBService.exe", "Lib\DBService.exe")
	} Else If FileExist(A_Temp "\yzime_sql.tmp")
		FileDelete %A_Temp%\yzime_sql.tmp
}
srfreload:
	If (MemoryDB)
		SaveDB(1)
	YzIni.Save(), hideorshowwindows("a"), DB.CloseDB()
	For Key, Value in srf_Plugins
		Process, Close, % Value[1]
	srf_Plugins:=YzIni:=""
	Try	{
		If A_IsCompiled
			Run "%A_ScriptFullPath%" /restart
		Else
			Run "%A_AhkPath%" /restart "%A_ScriptFullPath%"
	}
	ExitApp

EXIT:
	If (YzIni="")
		ExitApp
	If (MemoryDB)
		SaveDB(1)
	DB.CloseDB(), YzIni.Save(), hideorshowwindows("a")
	For Key, Value in srf_Plugins
		Process, Close, % Value[1]
	ExitApp

EmptyMem:
	If (A_TimeSincePriorHotkey>60000){
		EmptyMem()
		If (MemoryDB)
			SaveDB()
	}
Return

; 来源: http://www.autohotkey.com/forum/topic32876.html
EmptyMem(PID="AHK Rocks"){
	pid:=(pid="AHK Rocks") ? DllCall("GetCurrentProcessId") : pid
	h:=DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", pid)
	DllCall("SetProcessWorkingSetSize", "UInt", h, "Int", -1, "Int", -1)
	DllCall("CloseHandle", "Int", h)
}
CloseOtherYZ(){
	DetectHiddenWindows, On
	SetTitleMatchMode RegEx
	pid:=DllCall("GetCurrentProcessId")
	WinGet, id, List, yz\.ahk|yz\.exe|影子输入法(32|64)?\.exe ahk_class AutoHotkey
	Loop %id%
	{
		WinGet, t, PID, % "ahk_id" id%A_Index%
		If (t>0&&t!=pid)
			PostMessage, 0x111, 65405, , , ahk_pid%t%
	}
	DetectHiddenWindows, Off
	SetTitleMatchMode 1
}
; 切换键盘模式
SwitchToEngIME(){
	; 下方代码可只保留一个
	SwitchIME(0x04090804)
}
SwitchIME(dwLayout){
	ControlGetFocus, ctl, A
	SendMessage, 0x50, 0, %dwLayout%, %ctl%, A
}
SetIMEStatus(Status:=0,all:=0){
	local
	DetectHiddenWindows, On
	SetTitleMatchMode 3
	If (all){
		WinGet, imehwndlist, List, Default IME ahk_class IME
		Loop %imehwndlist%
			SendMessage 0x283, 0x06, %Status%, , % "ahk_id" imehwndlist%A_Index%
	} Else {
		WinGet, activeexe, ProcessName, % "ahk_id" (activehwnd:=WinExist("A"))
		WinGet, imehwndlist, List, Default IME ahk_class IME ahk_exe %activeexe%
		Loop %imehwndlist%
			If (DllCall("GetParent", "Ptr", imehwndlist%A_Index%)=activehwnd)
				SendMessage 0x283, 0x06, %Status%, , % "ahk_id" imehwndlist%A_Index%
	}
	DetectHiddenWindows, Off
	SetTitleMatchMode 1
	Sleep, 50
}
IME_GETOPENSTATUS(WinTitle="A"){	; 多进程输入状态优化
	local
	hWnd:=WinExist(WinTitle)
	WinGet, activeexe, ProcessName, ahk_id %hWnd%
	DetectHiddenWindows, ON
	WinGet, imehwndlist, List, Default IME ahk_class IME ahk_exe %activeexe%
	res:=0
	Loop %imehwndlist%
		If (DllCall("GetParent", "Ptr", imehwndlist%A_Index%)=hWnd){
			SendMessage 0x283, 0x05, 0, , % "ahk_id" imehwndlist%A_Index%	; Message : WM_IME_CONTROL  wParam:IMC_GETOPENSTATUS
			res |= ErrorLevel
		}
	; DefaultIMEWnd:=DllCall("imm32\ImmGetDefaultIMEWnd", "Uint", hWnd, "Uint")
	DetectHiddenWindows, Off
	Return res
}

; Include
#Include %A_ScriptDir%
#Include Lib\Gdip.ahk		; Gdip类库
#Include Lib\EasyIni.ahk	; ini类库
#Include Lib\Class_SQLiteDB.ahk	; SQLite类库
#Include Lib\Class_Everything.ahk	; Everything类库
#Include Lib\ToolTip.ahk	; ToolTip样式
#Include Lib\GdipText.ahk	; Gdip样式候选框
#Include Lib\srf_Init.ahk		; 初始化
#Include Lib\srf_IME.ahk		; 找词、功能跳转、显示候选
#Include Lib\srf_Func.ahk	; 输入法上屏等函数
#Include Lib\srf_Option.ahk	; 选项界面
#Include Lib\Ciku_Manager.ahk   ; 词库管理界面
#Include Lib\functions.ahk	; 内置命令模块
#Include Lib\COPYDATA.ahk	; WM_COPYDATA进程间通信
#Include Lib\suanfa_pinyin.ahk	; 拼音类算法
; #Include Lib\suanfa_sanma.ahk	; 二三码混打算法(2码+1辅助码)
#Include Lib\suanfa_xingma.ahk	; 非全拼类算法
#Include Lib\suanfa_lianda.ahk	;